import 'package:objectbox/objectbox.dart';

@Entity()
class CoinShopBox {
  @Id(assignable: true)
  int id = 0;

  final String discount; // 折扣
  final String exchangeCoin; // 兑换金币的数量
  final String goodsId; // 商品 id
  final String originalPrice; // 原价
  final String price; // 现价
  final String code; // 商品编号
  final String? tags; // 商品编号

  CoinShopBox({
    required this.id,
    required this.discount,
    required this.exchangeCoin,
    required this.goodsId,
    required this.originalPrice,
    required this.price,
    required this.code,
    required this.tags,
  });

  String getDiscount() {
    if (discount.isEmpty || discount == "0" || discount == "0.0" || 'null' == discount) {
      return "";
    }
    try {
      return "${(double.parse(discount) * 100).toInt()}% off";
    } catch (e) {
      return "$discount% off";
    }
  }

  String getOriginalPrice() {
    if (price == originalPrice) {
      return "";
    }
    return "\$$originalPrice";
  }

  String getPrice() => "\$$price";

  @override
  String toString() {
    return {
      'id': id,
      'discount': getDiscount(),
      'exchangeCoin': exchangeCoin,
      'goodsId': goodsId,
      'originalPrice': getOriginalPrice(),
      'price': getPrice(),
      'code': code,
      'tags': tags,
    }.toString();
  }
}
